// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// u [author] rhyme_lph
// d [email]  rhymelph@gmail.com
// W [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/provider/dashboard/credit_debit/select_item_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/reg_exp_utils.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/select/select_item_radio.dart';
import 'package:flutter/material.dart';

@RouterMeta(paramName: "selectItemPage")
class SelectItemPage extends StatefulWidget {
  final int billInvoiceId;
  final bool isCredit;

  const SelectItemPage({
    Key key,
    this.billInvoiceId,
    this.isCredit,
  }) : super(key: key);

  @override
  _SelectItemPageState createState() => _SelectItemPageState();
}

class _SelectItemPageState extends State<SelectItemPage>
    with PagePresenter<SelectItemPage, SelectItemProvider> {
  @override
  void initState() {
    provider.isCredit = widget.isCredit ?? false;
    provider.billInvoiceId = widget.billInvoiceId;
    super.initState();
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: 'Select Item',
      );

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            AppSpaces.h20,
            SearchInputField(
              controller: provider.searchController,
              focusNode: provider.searchFocusNode,
            ),
            AppSpaces.h20,
            AppSpaces.grayH05,
            Visibility(
              visible:
                  provider.items.length != null && provider.items.length > 0,
              child: listBuilder<String>(
                selector: (_, __) => __.searchController.text,
                builder: (BuildContext context, String value, Widget child) =>
                    Column(
                  children: provider.items
                      .where((element) => RegExpUtils.getSearchRegExp(value)
                          .hasMatch(element.name))
                      .map((e) => PressedOpacityButton(
                            onPressed: () {
                              provider.onSelectItem(e);
                            },
                            child: SelectItemRadio(
                              item: e,
                              padding: EdgeInsets.symmetric(
                                  vertical: 15, horizontal: 30),
                              isSelectMode: true,
                            ),
                          ))
                      .toList(),
                ),
              ),
              replacement: Padding(
                padding: const EdgeInsets.only(top: 180.0),
                child: super.buildEmptyWidget,
              ),
            ),
          ],
        ),
      );
}
